package day;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class MedianFinder {

    List<Integer> nums = null;

    /**
     * initialize your data structure here.
     */
    public MedianFinder() {
        nums = new ArrayList<Integer>();
    }

    public void addNum(int num) {
        int idx = Collections.binarySearch(nums, num);
        nums.add(-1 * idx - 1, num);
    }

    public double findMedian() {
        int size = nums.size();
        if (size == 0) {
            return 0;
        } else if (size % 2 == 0) {
            return (nums.get((size - 1) / 2) + nums.get(size / 2)) / 2.0;
        } else {
            return nums.get(size / 2);
        }
    }

    public static void main(String[] args) {
        List<Integer> nums = new ArrayList<>();
        nums.add(1);
        nums.add(2);
        int idx = Collections.binarySearch(nums, 0);
        System.out.println(idx * -1 - 1);
    }
}
